package java8.optional.demo03;

import java8.optional.Insurance;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Optional;

/**
 * @author ChangLiang
 * @date 2020/4/21
 */
public class AppTest {

    @Test
    @DisplayName("test flat map")
    public void test() {
        // output: unknown
        System.out.println(getInsuranceNameByOptional(null));

        System.out.println(getInsuranceNameByOptional(new Person()));
    }

    private String getInsuranceNameByOptional(Person person) {
        return Optional.ofNullable(person)
                .flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName).orElse("unknown");
    }
}
